package com.qcby.facory;

import com.qcby.proxy.CglibTransactionInterceptor;
import com.qcby.proxy.JdkTransactionHandler;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Proxy;

/**
 * @author 程子强
 * @Description
 * @date 2025/7/10 9:14
 */
//定义代理工厂，可以提供代理对象的
public class ProxyFactory {
    public static Object createProxy(Object target) {
        //先判断目标对象是否实现了接口来决定使用哪种代理
        if (target.getClass().getInterfaces().length > 0) {
            System.out.println("接口："+target.getClass().getInterfaces());
            return Proxy.newProxyInstance(
                    target.getClass().getClassLoader(),
                    target.getClass().getInterfaces(),
                    new JdkTransactionHandler(target)
            );
        } else {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(target.getClass());
            enhancer.setCallback(new CglibTransactionInterceptor());
            return enhancer.create();
        }
    }
}
